package presentation.windows;

import java.awt.Font ;
import java.rmi.RemoteException ;

import javax.swing.BorderFactory ;
import javax.swing.JButton ;
import javax.swing.JLabel ;
import javax.swing.JOptionPane ;
import javax.swing.JTextField ;
import javax.swing.SwingWorker ;
import javax.swing.border.BevelBorder ;

import util.Log ;
import util.NotFoundException ;
import util.Subsystem ;
import domain.user.UnpermittedActionException ;



/**
 * This is the admin mode screen used for giving moderator privileges to forum users
 * @author Owner
 *
 */
public class AdminPanel extends BasePanel
{

	private static final long serialVersionUID = -283581627785575927L ;

	private JLabel adminLabel;
	private JLabel orderLabel;
	private JTextField orderTextField;
	private JButton moderatorButton;

	/**
	 * Constructor
	 * @param sharedData - the data we need to pass in order to create the panel
	 */
	public AdminPanel(final SharedUIData sharedData)
	{
		super(sharedData);
		this.setPreferredSize(new java.awt.Dimension(735, 566));
		this.initGUI();
	}


	/**
	 * initializes the GUI of the screen
	 */
	private void initGUI()
	{
		this.adminLabel = new JLabel();
		this.add(this.adminLabel);
		this.adminLabel.setText("Admin Mode");
		this.adminLabel.setBounds(296, 49, 150, 32);
		this.adminLabel.setFont(new Font("Californian FB",1,22));

		this.orderLabel = new JLabel();
		this.add(this.orderLabel);
		this.orderLabel.setText("enter username you want to make moderator:");
		this.orderLabel.setFont(new Font("Californian FB",1,16));
		this.orderLabel.setBounds(50, 80, 342, 55);

		this.orderTextField = new JTextField();
		this.add(this.orderTextField);
		this.orderTextField.setBounds(253, 135, 166, 21);
		this.orderTextField.setBorder(BorderFactory.createCompoundBorder(
				null,
				BorderFactory.createBevelBorder(BevelBorder.LOWERED)));

		this.moderatorButton = new JButton();
		this.add(this.moderatorButton);
		this.moderatorButton.setText("Make Moderator");
		this.moderatorButton.setBounds(269, 316, 150, 35);

		//set button handlers:
		this.moderatorButton.addActionListener(new java.awt.event.ActionListener() {
	         @SuppressWarnings("synthetic-access")
			public void actionPerformed(@SuppressWarnings("unused") final java.awt.event.ActionEvent evt) {
	        	 new SwingWorker<Void,Void>(){
	     			@Override
	     			protected Void doInBackground(){
	     				AdminPanel.this.modaratorButtonHandler();
	     				return null;
	     			}
	     			@Override
	    			protected void done() { /* there is nothing to do here .. */ }
	    		}.execute();
	         }});
	}
	/**
	 * button handler
	 */
	private void modaratorButtonHandler()
	{
		String userName = this.orderTextField.getText();
		if (userName==null || userName.equals(""))
		{
			JOptionPane.showMessageDialog(AdminPanel.this,
				"missing username!", "error occured", JOptionPane.ERROR_MESSAGE);
			this.orderTextField.setText("");
			return;
		}
		try
		{
			this.forum.addModeratorPrivileges(this.getSharedData().getUser().getId(), userName);
			JOptionPane.showMessageDialog(AdminPanel.this,
				"user " + userName + " was made forum moderator");
			Log.getLogger(Subsystem.PRESENTATION_MODEL).finest("user " + userName + " was made forum moderator");
			this.notifyCompleted();
		}
		catch (RemoteException e)
		{
			JOptionPane.showMessageDialog(AdminPanel.this,
				"communication problem-please try again", "error occured", JOptionPane.ERROR_MESSAGE);
			this.orderTextField.setText("");
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("remote exception") ;

		}
		catch (NotFoundException e)
		{
			JOptionPane.showMessageDialog(AdminPanel.this,
				"username not found - enter correct username", "error occured", JOptionPane.ERROR_MESSAGE);
			this.orderTextField.setText("");
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("username  not in system. maybe typed incorrect user") ;
		}
		catch (UnpermittedActionException e)
		{
			JOptionPane.showMessageDialog(AdminPanel.this,
				"dont have permission", "error occured", JOptionPane.ERROR_MESSAGE);
			this.orderTextField.setText("");
			Log.getLogger(Subsystem.PRESENTATION_MODEL).info("unpermitted action exception") ;
		}

	}






}